var gulp = require('gulp'),
    iconfont = require('gulp-iconfont'),
    iconfontCss = require('gulp-iconfont-css'),
    clean = require("gulp-clean"),
    taskListing = require("gulp-task-listing"),
    template = require("gulp-template"),
    gulpconcat = require('gulp-concat'),                          //- 多个文件合并为一个；
    gulprename = require('gulp-rename'),
    cssclean = require('gulp-clean-css'),                    //- 压缩CSS
    base64 = require('gulp-base64'),
    fs = require("fs"),
    icons = fs.readdirSync("src/icons");

icons = icons.map(function (icon) {
    return icon.replace(/\.\w+$/, '');
});
var iconGroup = {
    1:{
        name:"常用图标",
        prefix:"common"
    },
    2:{
        name:"报销图标",
        prefix:"reim"
    }
};

var fontName = 'iconfont';
gulp.task('clean', function () {
    return gulp.src("./build", {read: false}).pipe(clean());
});
gulp.task('iconfont', ['clean'], function () {
    //在build/css下生成fonticon.css
    console.log("开始生成fonticon.css")
    return gulp.src('src/icons/*.svg')
        .pipe(iconfontCss({
            fontName: {
                name:fontName,
                iconGroup:iconGroup
            },
            path: 'src/templates/css/_icons.css',
            targetPath: '../css/ecmp-all.css',
            fontPath: '../fonts/',
        }))
        .pipe(iconfont({
            fontName: fontName,
            formats: ['svg', 'ttf', 'eot', 'woff', 'woff2'],
            normalize: true,
            //fontHeight: 1001
            //  options: {
            //      fixedWidth: false,
            //      normalize: false,
            //      fontHeight: 512,
            //      descent: -32,
            //      normalize:true
            //  }
        }))
        .pipe(gulp.dest('./build/fonts/'));
});
//生成文档
gulp.task('doc', ['clean'], function () {
    gulp.src('src/templates/view/icon.html')
        .pipe(template({icons: icons, iconGroup:iconGroup}))
        .pipe(gulp.dest("./views"));
});
gulp.task('do', ['clean','iconfont', 'doc'], function () {
    console.log("开始合并压缩样式文件")
    gulp.src(['build/css/ecmp-all.css', 'src/defaultCss/*.css'])
        .pipe(gulpconcat('all.css'))
        .pipe(base64())
        .pipe(gulprename('ecmp-all-min.css'))
        .pipe(cssclean({
            compatibility: 'ie8'
        }))
        .pipe(gulp.dest('static/css/'));
    console.log("操作成功");
});


